package com.humble.humbleojserviceclient.service;

import cn.hutool.core.collection.CollUtil;
import com.humble.humbleojcommon.common.ErrorCode;
import com.humble.humbleojcommon.exception.BusinessException;
import com.humble.humbleojmodel.model.entity.User;
import com.humble.humbleojmodel.model.enums.UserRoleEnum;
import com.humble.humbleojmodel.model.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.humble.humbleojcommon.constant.UserConstant.USER_LOGIN_STATE;

/**
 * 用户内部调用接口服务
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@FeignClient(name = "humbleoj-backend-user-service",path = "/api/user/inner")
public interface UserFeignClient {

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    default User getLoginUser(HttpServletRequest request){
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) userObj;
        if (currentUser == null || currentUser.getId() == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        // 从数据库查询（追求性能的话可以注释，直接走缓存）
        // long userId = currentUser.getId();
        // currentUser = this.getById(userId);
        // if (currentUser == null) {
        //     throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        // }
        return currentUser;
    }

    /**
     * 是否为管理员
     *
     * @param request
     * @return
     */

    default boolean isAdmin(HttpServletRequest request){
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        return isAdmin(user);
    }

    /**
     * 是否为管理员
     *
     * @param user
     * @return
     */
    default boolean isAdmin(User user){
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }

    /**
     * 获取脱敏的用户信息
     *
     * @param user
     * @return
     */
    default UserVO getUserVO(User user){
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }


    /**
     * 获取脱敏的用户信息
     *
     * @param userList
     * @return
     */
    default List<UserVO> getUserVO(List<User> userList){
        if (CollUtil.isEmpty(userList)) {
            return new ArrayList<>();
        }
        return userList.stream().map(this::getUserVO).collect(Collectors.toList());
    }

    //通过id获取用户
    @GetMapping("/get/id")
    User getById(@RequestParam("id") Serializable id);

    @GetMapping("/get/ids")
    List<User> listByIds(@RequestParam("idList") Collection<? extends Serializable> idList);

}
